using System.Windows.Forms;
using System.Drawing;
using System;

namespace Jonathan.ColorPicker
{
    public class ToolStripColorDropDown : ToolStripDropDown
    {
        private OfficeColorPicker _colorPicker;

        public ToolStripColorDropDown ()
        {
            this._colorPicker = new OfficeColorPicker(Color.Black);
            this._colorPicker.Width = OfficeColorPicker.PreferredWidth;
            this._colorPicker.Height = OfficeColorPicker.PreferredHeight;
            Items.Add(new ToolStripControlHost(_colorPicker));

            this._colorPicker.SelectedColorChanged += new EventHandler(_colorPicker_SelectedColorChanged);
        }

        void _colorPicker_SelectedColorChanged (object sender, EventArgs e)
        {
            this.Close(ToolStripDropDownCloseReason.CloseCalled);
        }

        protected override void OnOpening (System.ComponentModel.CancelEventArgs e)
        {
            base.OnOpening(e);

            ToolStripProfessionalRenderer renderer = Renderer as ToolStripProfessionalRenderer;

            if (renderer != null)
                _colorPicker.BackColor = renderer.ColorTable.ToolStripDropDownBackground;
        }

        protected override void OnOpened (EventArgs e)
        {
            base.OnOpened(e);
            _colorPicker.Focus();
        }

        /// <summary>
        /// Gets the Color Picker...
        /// </summary>
        public OfficeColorPicker ColorPicker
        {
            get { return this._colorPicker; }
        }
    }
}
